package com.hhtc.jsj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhtc.jsj.common.utils.ShiroUtil;
import com.hhtc.jsj.entity.PostLike;
import com.hhtc.jsj.entity.User;
import com.hhtc.jsj.mapper.PostLikeMapper;
import com.hhtc.jsj.request.PostLikeRequest;
import com.hhtc.jsj.request.UserRequest;
import com.hhtc.jsj.service.PostCommentService;
import com.hhtc.jsj.service.PostLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhtc.jsj.service.PostService;
import com.hhtc.jsj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 点赞表 服务实现类
 * </p>
 *
 * @author ty
 * @since 2022-04-07
 */
@Slf4j
@Service
public class PostLikeServiceImpl extends ServiceImpl<PostLikeMapper, PostLike> implements PostLikeService {
    @Autowired
    private UserService userService;
    @Autowired
    private PostService postService;
    @Autowired
    private PostCommentService postCommentService;
    @Override
    @Transactional
    public boolean add(PostLikeRequest postLikeRequest) {
        postLikeRequest.setId(IdWorker.getId());
        postLikeRequest.setLikeUserId(ShiroUtil.getProfile().getId());
        postLikeRequest.setCreatedTime(System.currentTimeMillis());
        postLikeRequest.setCreatedBy(ShiroUtil.getProfile().getId().toString());
        postLikeRequest.setUpdatedTime(System.currentTimeMillis());
        postLikeRequest.setUpdatedBy(ShiroUtil.getProfile().getId().toString());
        PostLike postLike = BeanUtil.copyProperties(postLikeRequest, PostLike.class);
        return this.save(postLike);
    }

    @Override
    @Transactional
    public boolean remove(PostLikeRequest postLikeRequest) {
        Long id = postLikeRequest.getId();
        return this.removeById(id);
    }

    @Override
    @Transactional
    public boolean update(PostLikeRequest postLikeRequest) {
        postLikeRequest.setUpdatedTime(System.currentTimeMillis());
        postLikeRequest.setUpdatedBy(ShiroUtil.getProfile().getId().toString());
        PostLike postLike = BeanUtil.copyProperties(postLikeRequest, PostLike.class);
        return this.updateById(postLike);
    }

    @Override
    public PostLikeRequest getById(PostLikeRequest postLikeRequest) {
        Long id = postLikeRequest.getId();
        PostLike postLike = this.getById(id);
        if (postLike != null) {
            return BeanUtil.copyProperties(postLike, PostLikeRequest.class);
        }
        return null;
    }

    @Override
    public List<PostLikeRequest> queryList(PostLikeRequest postLikeRequest) {
        QueryWrapper<PostLike> queryWrapper = new QueryWrapper<>();
        this.buildWrapper(queryWrapper, postLikeRequest);
        List<PostLike> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        return BeanUtil.copyToList(list, PostLikeRequest.class);
    }

    @Override
    public Page queryPage(PostLikeRequest postLikeRequest) {
        Page<PostLike> page = new Page<>();
        QueryWrapper<PostLike> wrapper = new QueryWrapper<PostLike>();
        buildWrapper(wrapper, postLikeRequest);
        Page<PostLike> pageResult = this.page(page, wrapper);
        return pageResult;
    }
    private void buildWrapper(QueryWrapper<PostLike> wrapper, PostLikeRequest param) {
        log.info("buildPageWrapper param :{}", param);
        Long id = param.getId();
        if (id != null) {
            wrapper.eq("id", id);
        }
        Long relationId = param.getRelationId();
        if (relationId != null) {
            wrapper.eq("relation_id", relationId);
        }
        Long likedUserId = param.getLikedUserId();
        if (likedUserId != null) {
            wrapper.eq("liked_user_id", likedUserId);
        }
        Long likeUserId = param.getLikeUserId();
        if (likeUserId != null) {
            wrapper.eq("like_user_id", likeUserId);
        }
        String likeUserName = param.getLikeUserName();
        if (StringUtils.isNotBlank(likeUserName)) {
            wrapper.eq("like_user_name", likeUserName);
        }
        String likeHeadImages = param.getLikeHeadImages();
        if (StringUtils.isNotBlank(likeHeadImages)) {
            wrapper.eq("like_head_images", likeHeadImages);
        }
        Integer isRead = param.getIsRead();
        if (isRead != null) {
            wrapper.eq("is_read", isRead);
        }
        Integer isCancel = param.getIsCancel();
        if (isCancel != null) {
            wrapper.eq("is_cancel", isCancel);
        }
    }
}
